<?php
class DatabaseObject_Article extends DatabaseObject {
	
	public $profile = null;
	const STATUS_DRAFT = 0;
	const STATUS_LIVE = 1;
	public function __construct($db) {
		parent::__construct($db, 'articles', 'articleid');
		
		$this->add('userid');
		$this->add('dateline', time());
		$this->add('status', self::STATUS_DRAFT);
		$this->add('friend', 0);
		$this->add('subject', '');
		$this->add('content');
		$this->add('publiship', '');
		$this->add('readnum', 0);
		$this->add('repnum', 0);
		
		$this->profile = new Profile_Article($db);
	}
	
	protected function postLoad() {
		$this->profile->setArticleId($this->getId());
		$this->profile->load();
	}
	
	protected function postInsert() {
		$this->profile->setArticleId($this->getId());
		$this->profile->save(false);
		return true;
	}
	
	protected function postUpdate() {
		$this->profile->save(false);
		return true;
	}
	
	protected function preDelete() {
		$this->profile->delete();
		return true;
	}
	
	public function loadForUser($userid, $articleid) {
		$articleid = (int) $articleid;
		$userid = (int) $userid;
		
		if ($articleid <= 0 || $userid <= 0) {
			return false;
		}
		
		$query = sprintf('select %s from %s where userid = %d and articleid = %d', 
		      join(',', $this->getSelectFields()), $this->_table, $userid, $articleid);
		
		return $this->_load($query);
	}
	
	public function sendLive() {
		if ($this->status != self::STATUS_LIVE) {
			$this->status = self::STATUS_LIVE;
			$this->profile->publiced = time();
		}
	}
	
	public function isLive() {
		return $this->isSaved() && $this->status == self::STATUS_LIVE;
	}
	
	public function sendBackToDraft() {
		$this->status = self::STATUS_DRAFT;
	}
	
    public function getTeaser($length) {
        require_once('Smarty/plugins/modifier.truncate.php');
        return smarty_modifier_truncate(strip_tags($this->content), $length);
    }
	
	static public function _GetBaseQuery($db, $options) {
		$defaults = array('userid' => array(),
		                  'from'   => '',
		                  'to'     => '');
		
		foreach ($defaults as $k => $v) {
			$options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
		}
		
		$select = $db->select();
		$select->from(array('a' => 'articles'), array());
		
		if (strlen($options['from']) > 0) {
			$select->where('a.dateline <= ?', $options['from']);
		}
		
		if (strlen($options['to']) > 0) {
			$select->where('a.dateline >= ?', $options['to']);
		}
		
		if (count($options['userid']) > 0) {
			$select->where('a.userid in (?)', $options['userid']);
		}
		return $select;
	}
	
	static public function GetArticleCount($db, $options) {
		$select = self::_GetBaseQuery($db, $options);
		$select->from(null, 'count(*)');
		
		return $db->fetchOne($select);
	}
	
	static public function GetMonthlySummary($db, $options) {
		if ($db instanceof Zend_Adapter_Pdo_Mysql) {
			$dateString = "date_format(a.dateline, '%Y-%m')";
		} else {
			$dateString = "to_char(a.dateline, 'yyyy-mm')";
		}
		
		$defaults = array('offset' => 0,
		                  'limit'  => 0,
		                  'order'  => $dateString.'desc'
		                  );
		                  
		foreach ($defaults as $k => $v) {
			$options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
		}
		
		$select = self::_GetBaseQuery($db, $options);
		$select->from(null, array($dateString.'as month', 'count(*) as num_articles'));
		$select->group($dateString);
		$select->order($options['order']);
		return $db->fetchPairs($select);
	}
	
	
	static public function GetArticles($db, $options = array()) {
		$defaults = array('offset' => 0,
		                  'limit'  => 0,
		                  'order'  => 'a.dateline');
		
		foreach ($defaults as $k => $v) {
			$options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
		}
		
		$select = self::_GetBaseQuery($db, $options);
		$select->from(null, 'a.*');
		
		if ($options['limit'] > 0) {
			$select->limit($options['limit'], $option['offset']);
		}
		
		$select->order($options['order']);
		
		$data = $db->fetchAll($select);
		$articles = self::BuildMultiple($db, __CLASS__, $data);
		$articleids = array_keys($articles);
	    if (count($articleids) == 0) {
            return array();
        }
		
		$profiles = Profile::BuildMultiple($db, 'Profile_Article', 
		      array('articleid' => $articleids));
		
		foreach ($articles as $articleid => $article) {
			if (array_key_exists($articleid, $profiles) 
			     && $profiles[$articleid] instanceof Profile_Article) {
			     $articles[$articleid]->profile = $profiles[$articleid];
			} else {
				$articles[$articleid]->profile->setArticleId($articleid);
			}
		}
		return $articles;
	}
}
?>